﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SYcwpb.ViewModels;
using System.Data;
using SYcwpb.Helpers;
using SYcwpb.Controls;
using System.Collections;

namespace SYcwpb.Views
{
    /// <summary>
    /// MoveShift.xaml 的交互逻辑
    /// </summary>
    public partial class MoveShift : Window
    {
        private DriverTeamViewModel _viewMode;
        public MoveShift()
        {
            InitializeComponent();
          
        }
        public MoveShift(DriverTeamViewModel ss)
        {
            this._viewMode = ss;
            InitializeComponent();
        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            //验证
            if (dpSourceDay.SelectedDate == null)
            {
                MessageBox.Show("请选择调班日期");
                return;
            }
            if (cmbDriver.SelectedValue == null)
            {
                MessageBox.Show("请选择调班人员");
                return;
            }
            if (cmbReplaceDriver.SelectedValue == null)
            {
                MessageBox.Show("请选择替换人员");
                return;
            }
            if (dpTagerDay.SelectedDate == null)
            {
                MessageBox.Show("请选择还班日期");
                return;
            }
            if (cmbAppBackcls.SelectedValue == null)
            {
                MessageBox.Show("请选择申请司机还班时的班次");
                return;
            }
            if (cmbTAppBackcls.SelectedValue==null)
            {
                MessageBox.Show("请选择替班司机还班时的班次");
                return;
            }
            //校验还班时的班次

            DataTable dtBackappDateSourceDriver = _viewMode.GetCrsInfoById(cmbAppBackcls.SelectedValue.ToString());
            DataTable dtBackappDateChangeDrive = _viewMode.GetCrsInfoById(cmbTAppBackcls.SelectedValue.ToString());

            if (dtBackappDateSourceDriver.Rows.Count == 0)
            {
                MessageBox.Show("查找不到申请司机还班时的调班记录");
                return;
            }
            if (dtBackappDateChangeDrive.Rows.Count == 0)
            {
                MessageBox.Show("查找不到代班司机还班时的调班记录");
                return;
            }

            string backDateSourceDrive = dtBackappDateSourceDriver.Rows[0]["dcdr_cls_id"].ToString();

            string backDateChangeDrive = dtBackappDateChangeDrive.Rows[0]["dcdr_cls_id"].ToString();

            if (backDateChangeDrive == "0")
            {
               
                    MessageBox.Show("还班时调班司机为早班，不能申请换班");
                    return;
               
            }
            if (backDateChangeDrive == "1")
            {
                MessageBox.Show("还班时调班司机为休班，不能申请调班");
                return;
            }
            if (backDateChangeDrive == "2")
            {
                if (backDateSourceDrive != "1")
                {  
                        MessageBox.Show("还班时白班只能与休班司机换班");
                        return;
                   
                }
            }
            if (backDateChangeDrive == "3")
            {
                if (backDateSourceDrive != "0")
                {
                    
                        MessageBox.Show("还班时晚班只能与早班司机换班");
                        return;
                    
                }
            }

            //判断申请调班司机和申请司机对不对
            DataTable dt = _viewMode.GetSwitchRecByEideCls(cmbDriver.SelectedValue.ToString(),cmbReplaceDriver.SelectedValue.ToString(), Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd"));
            if (dt.Rows.Count > 0)
            {
                //判断还班日期
                if (Convert.ToDateTime(dt.Rows[0]["sr_appdate"]).AddDays(30) < Convert.ToDateTime(dpTagerDay.SelectedDate))
                {
                    MessageBox.Show("还班时间必须在调班后30天以内！");
                    return;
                }
                //取消
                ArrayList lstSql = new ArrayList();
                //取消原先的调班
              string res= _viewMode.CancelBackExchangeDriver(cmbDriver.SelectedValue.ToString(), Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd"),ref lstSql);
              if (res == "2")
              {
                  MessageBox.Show("调班记录无还班日期 无法修改调班");
                  return;
              }
                //修改新的调班
                string strAppBackcls = "";
                string strTAppBackcls = "";
                if (cmbAppBackcls.SelectedValue!= null)
                {
                    strAppBackcls = cmbAppBackcls.SelectedValue.ToString();
                }
                if (cmbTAppBackcls.SelectedValue!= null)
                {
                    strTAppBackcls = cmbTAppBackcls.SelectedValue.ToString();
                }
                 ArrayList lstSql1 = new ArrayList();
              bool bres= _viewMode.setBackClsInfo(strAppBackcls, strTAppBackcls,Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd"), cmbDriver.SelectedValue.ToString(), cmbReplaceDriver.SelectedValue.ToString(), lstSql1,dt.Rows[0][0].ToString());
                //再进行调班
              if (bres)
              {
                  _viewMode.ExecuteSqlTran(lstSql);
                  _viewMode.ExecuteSqlTran(lstSql1);
                  MessageBox.Show("修改还班成功");
              }
              
               
                //if (_viewMode.ExchangeDriverNew(cmbAppCls.SelectedValue.ToString(), cmbTAppcls.SelectedValue.ToString(), strAppBackcls, strTAppBackcls, sourceDate, tagerDate, txtReson.Text,cmbDriver.SelectedValue.ToString(),cmbReplaceDriver.SelectedValue.ToString()))
            }
            else
            {
                MessageBox.Show("申请者与替代者没有可修改的调班记录！");
            }
           


            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = _viewMode;
            List<Driver> lstDriver = _viewMode.InitDriverComboBox();
            cmbDriver.ItemsSource = lstDriver;
            cmbDriver.SelectedValuePath = "userid";
            cmbDriver.DisplayMemberPath = "userViewName";
            cmbReplaceDriver.ItemsSource = lstDriver;
            cmbReplaceDriver.SelectedValuePath = "userid";
            cmbReplaceDriver.DisplayMemberPath = "userViewName";
            dpSourceDay.DisplayDateStart = DateTime.Now;
            dpTagerDay.DisplayDateStart=DateTime.Now;
            //cmbReplaceDriver.ItemsSource=
        }


        private void button1_ClickNew(object sender, RoutedEventArgs e)
        {
            //1.判断所有选择的数据是否都已经选择了 （调班日期 申请调班人员 替班人员 ）
            //2.判断还班时间是否正确
            //3.判断班次是否正确
            //4.判断是否有调班记录 是否删除
            //5.开始调班

            

        }
       

        private void cmbDriver_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //
            if (dpSourceDay.SelectedDate != null && cmbDriver.SelectedValue!=null)
            {
                labApplycls.Content = _viewMode.GetCrossRemark(Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd"), cmbDriver.SelectedValue.ToString());

                cmbAppCls.ItemsSource = _viewMode.GetCrsInfo(cmbDriver.SelectedValue.ToString(), Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd")).DefaultView;
                cmbAppCls.SelectedValuePath = "ID";
                cmbAppCls.DisplayMemberPath = "dcdr_duty";
            }
            if (dpTagerDay.SelectedDate != null && isBlackClass.IsChecked == true && cmbDriver.SelectedValue != null)
            {
                labapplyBackCls.Content = _viewMode.GetCrossRemark(Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd"), cmbDriver.SelectedValue.ToString());

                cmbAppBackcls.ItemsSource = _viewMode.GetCrsInfoByBack(cmbDriver.SelectedValue.ToString(), Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd")).DefaultView;
                cmbAppBackcls.SelectedValuePath = "ID";
                cmbAppBackcls.DisplayMemberPath = "dcdr_duty";
                
            }
        }

        private void dpTagerDay_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            //List<Driver> lstDriver = _viewMode.InitDriverComboBox(dpTagerDay.SelectedDate.ToString());
            //cmbDriver.ItemsSource = lstDriver;
            //cmbDriver.SelectedValuePath = "userid";
            //cmbDriver.DisplayMemberPath = "userViewName";

            if (cmbDriver.SelectedValue != null && isBlackClass.IsChecked == true && cmbDriver.SelectedValue!=null)
            {
                labapplyBackCls.Content = _viewMode.GetCrossRemark(Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd"), cmbDriver.SelectedValue.ToString());


                cmbAppBackcls.ItemsSource = _viewMode.GetCrsInfoByBack(cmbDriver.SelectedValue.ToString(), Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd")).DefaultView;
                cmbAppBackcls.SelectedValuePath = "ID";
                cmbAppBackcls.DisplayMemberPath = "dcdr_duty";
            }
            if (cmbReplaceDriver.SelectedValue != null && isBlackClass.IsChecked == true)
            {
                labbackcls.Content = _viewMode.GetCrossRemark(Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd"), cmbReplaceDriver.SelectedValue.ToString());

                cmbTAppBackcls.ItemsSource = _viewMode.GetCrsInfo(cmbReplaceDriver.SelectedValue.ToString(), Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd")).DefaultView;
                cmbTAppBackcls.SelectedValuePath = "ID";
                cmbTAppBackcls.DisplayMemberPath = "dcdr_duty";
            }
        }

        private void cmbReplaceDriver_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dpSourceDay.SelectedDate != null && cmbReplaceDriver.SelectedValue!=null)
            {
                labReplacecls.Content = _viewMode.GetCrossRemark(Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd"), cmbReplaceDriver.SelectedValue.ToString());


                cmbTAppcls.ItemsSource = _viewMode.GetCrsInfoByBack(cmbReplaceDriver.SelectedValue.ToString(), Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd")).DefaultView;
                cmbTAppcls.SelectedValuePath = "ID";
                cmbTAppcls.DisplayMemberPath = "dcdr_duty";

            }
            if (dpTagerDay.SelectedDate != null && isBlackClass.IsChecked == true && cmbReplaceDriver.SelectedValue!=null)
            {
                labbackcls. Content = _viewMode.GetCrossRemark(Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd"), cmbReplaceDriver.SelectedValue.ToString());

                cmbTAppBackcls.ItemsSource = _viewMode.GetCrsInfo(cmbReplaceDriver.SelectedValue.ToString(), Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd")).DefaultView;
                cmbTAppBackcls.SelectedValuePath = "ID";
                cmbTAppBackcls.DisplayMemberPath = "dcdr_duty";
            }
        }

        private void dpSourceDay_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbDriver.SelectedValue != null && cmbDriver.SelectedValue!=null)
            {
                labApplycls.Content = _viewMode.GetCrossRemark(Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd"), cmbDriver.SelectedValue.ToString());

                cmbAppCls.ItemsSource = _viewMode.GetCrsInfo(cmbDriver.SelectedValue.ToString(), Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd")).DefaultView;
                cmbAppCls.SelectedValuePath = "ID";
                cmbAppCls.DisplayMemberPath = "dcdr_duty";
            }
            if (cmbReplaceDriver.SelectedValue != null && cmbReplaceDriver.SelectedValue!=null)
            {
                labReplacecls.Content = _viewMode.GetCrossRemark(Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd"), cmbReplaceDriver.SelectedValue.ToString());


                cmbTAppcls.ItemsSource = _viewMode.GetCrsInfoByBack(cmbReplaceDriver.SelectedValue.ToString(), Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd")).DefaultView;
                cmbTAppcls.SelectedValuePath = "ID";
                cmbTAppcls.DisplayMemberPath = "dcdr_duty";
            }

            //设置还班日期
            if (dpSourceDay.SelectedDate != null)
            {
                dpTagerDay.SelectedDate = null;
                dpTagerDay.DisplayDateStart = Convert.ToDateTime(dpSourceDay.SelectedDate);
                dpTagerDay.DisplayDateEnd = Convert.ToDateTime(dpSourceDay.SelectedDate).AddDays(30);
            }

        }

        private void isBlackClass_Checked(object sender, RoutedEventArgs e)
        {
           
        }

        private void isBlackClass_Click(object sender, RoutedEventArgs e)
        {
            if (isBlackClass.IsChecked == true && dpTagerDay.SelectedDate!=null)
            {
                if (cmbDriver.SelectedValue != null && isBlackClass.IsChecked == true )
                {
                    labapplyBackCls.Content = _viewMode.GetCrossRemark(Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd"), cmbDriver.SelectedValue.ToString());

                    cmbAppBackcls.ItemsSource = _viewMode.GetCrsInfoByBack(cmbDriver.SelectedValue.ToString(), Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd")).DefaultView;
                    cmbAppBackcls.SelectedValuePath = "ID";
                    cmbAppBackcls.DisplayMemberPath = "dcdr_duty";
                }
                if (cmbReplaceDriver.SelectedValue != null && isBlackClass.IsChecked == true)
                {
                    labbackcls.Content = _viewMode.GetCrossRemark(Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd"), cmbReplaceDriver.SelectedValue.ToString());

                    cmbTAppBackcls.ItemsSource = _viewMode.GetCrsInfo(cmbReplaceDriver.SelectedValue.ToString(), Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd")).DefaultView;
                    cmbTAppBackcls.SelectedValuePath = "ID";
                    cmbTAppBackcls.DisplayMemberPath = "dcdr_duty";
                }
            }
            else
            {

                labapplyBackCls.Content = "";

                labbackcls.Content = "";
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            //是否是休息人员
           // bool isRest = false;
            if (cmbDriver.SelectedValue == null)
            {
                MessageBox.Show("请选择取消申请调班人");
                return;
            }
            if (dpSourceDay.SelectedDate == null)
            {
                MessageBox.Show("请选择取消调班日期");
                return;
            }

            if (_viewMode.CancelExchangeDriverNew(cmbDriver.SelectedValue.ToString(), Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd")))
            {
                MessageBox.Show("取消调班成功");
               
            }
            else
            {
                MessageBox.Show("当天该司机没有调班记录！");
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {


            if (dpSourceDay.SelectedDate == null)
            {
                MessageBox.Show("请选择调班日期");
                return;
            }
            if (cmbDriver.SelectedValue == null)
            {
                MessageBox.Show("请选择调班人员");
                return;
            }
            if (cmbReplaceDriver.SelectedValue == null)
            {
                MessageBox.Show("请选择替换人员");
                return;
            }
            if (isBlackClass.IsChecked == true)
            {
                if (dpTagerDay.SelectedDate == null)
                {
                    MessageBox.Show("请选择还班日期");
                    return;
                }

                //判断还班日期

                DateTime applyDate = Convert.ToDateTime(dpSourceDay.SelectedDate);
                DateTime backDate = Convert.ToDateTime(dpTagerDay.SelectedDate);

                if (applyDate.AddDays(30) < backDate)
                {
                    MessageBox.Show("还班日期不在规定时间内！");
                    return;
                }

                //if (applyDate.Day <= 10)
                //{
                //    //月初
                //    if (applyDate.Year == backDate.Year && applyDate.Month == backDate.Month && backDate.Day <= 20)
                //    { }
                //    else
                //    {
                //        MessageBox.Show("还班日期不在规定时间内！");
                //        return;
                //    }
                //}
                //else if (applyDate.Day <= 20)
                //{
                //    //如果在月中 那么在月未还
                //    if (applyDate.Year == backDate.Year && applyDate.Month == backDate.Month && backDate.Day <= 31)
                //    { }
                //    else
                //    {
                //        MessageBox.Show("还班日期不在规定时间内！");
                //        return;
                //    }
                //}
                //else
                //{
                //    //如果在月未 那么在下月月头还
                //    if ((applyDate.Year == backDate.Year && applyDate.Month == backDate.Month && backDate.Day <= 31) || (applyDate.AddMonths(1).Month == backDate.Month && backDate.Day <= 10))
                //    { }
                //    else
                //    {
                //        MessageBox.Show("还班日期不在规定时间内！");
                //        return;
                //    }
                //}


                //如果是月初 那么在月中还





            }
            string sourceDate = Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd");
            string tagerDate = "";
            if (dpTagerDay.SelectedDate != null && isBlackClass.IsChecked == true)
            {
                tagerDate = Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd");
            }
          


            //判断申请人是不是早班 如果是早班 不给申请

            //申请那天司机的班次
            //string appDateSourceDrive = _viewMode.DriverClsType(cmbDriver.SelectedValue.ToString(), Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd"));
            ////申请当天 替班司机的 班次
            //string appDateChangeDrive = _viewMode.DriverClsType(cmbReplaceDriver.SelectedValue.ToString(), Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd"));
            if(cmbAppCls.SelectedValue==null)
            {
                
                MessageBox.Show("请选择申请人所属班次");
                return;
            }

         
            if(cmbTAppcls.SelectedValue==null)
            {
                MessageBox.Show("请选择代班人所属班次");
                return;
            }
            DataTable dtappDateSourceDriver=_viewMode.GetCrsInfoById(cmbAppCls.SelectedValue.ToString());
            DataTable dtappDateChangeDrive=_viewMode.GetCrsInfoById(cmbTAppcls.SelectedValue.ToString());

            if(dtappDateSourceDriver.Rows.Count==0)
            {
                MessageBox.Show("查找不到申请司机的调班记录");
                return;
            }
            if(dtappDateChangeDrive.Rows.Count==0)
            {
                MessageBox.Show("查找不到申请司机的调班记录");
                return;
            }

            string appDateSourceDrive = dtappDateSourceDriver.Rows[0]["dcdr_cls_id"].ToString();

            string appDateChangeDrive = dtappDateChangeDrive.Rows[0]["dcdr_cls_id"].ToString();

            if (appDateSourceDrive == appDateChangeDrive)
            {
                //判断替班司机的班次是不是车场司机
               if( dtappDateChangeDrive.Rows[0]["dcdr_crosstype"].ToString()=="5")
                {
                    if (isBlackClass.IsChecked == false)
                    {
                        MessageBox.Show("同组司机调班 请选择还班");
                        return;
                    }
                    else
                    {
                        if (sourceDate == tagerDate)
                        { }
                        else
                        {
                            MessageBox.Show("同组司机调班 还班日期必须和调班日期相同");
                            return;
                        }
                    }
                }
            }

            if (appDateSourceDrive == "0")
            {
                if (appDateChangeDrive == "0")
                { }
                else
                {
                    MessageBox.Show("早班司机不能申请调班");
                    return;
                }
            }
            if (appDateSourceDrive == "1")
            {
                MessageBox.Show("休班司机不能申请调班");
                return;
            }
            if (appDateSourceDrive == "2")
            {
                if (appDateChangeDrive != "1")
                {
                    if (appDateChangeDrive != "2")
                    {
                        MessageBox.Show("白班只能与休班司机换班");
                        return;
                    }
                }
            }
            if (appDateSourceDrive == "3")
            {
                if (appDateChangeDrive != "0")
                {
                    if (appDateChangeDrive != "3")
                    {
                        MessageBox.Show("晚班只能与早班司机换班");
                        return;
                    }
                }
            }

            //判断是否需要还班
            if (isBlackClass.IsChecked == true)
            {
                //string backDateSourceDrive = _viewMode.DriverClsType(cmbDriver.SelectedValue.ToString(), Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd"));
                ////申请当天 替班司机的 班次
                //string backDateChangeDrive = _viewMode.DriverClsType(cmbReplaceDriver.SelectedValue.ToString(), Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd"));

                if (cmbAppBackcls.SelectedValue == null)
                {
                    MessageBox.Show("请选择申请人还班时所属班次");
                    return;
                }

                if (cmbTAppBackcls.SelectedValue == null)
                {
                    MessageBox.Show("请选择代班人还班时所属班次");
                    return;
                }
                DataTable dtBackappDateSourceDriver = _viewMode.GetCrsInfoById(cmbAppBackcls.SelectedValue.ToString());
                DataTable dtBackappDateChangeDrive = _viewMode.GetCrsInfoById(cmbTAppBackcls.SelectedValue.ToString());

                if (dtBackappDateSourceDriver.Rows.Count == 0)
                {
                    MessageBox.Show("查找不到申请司机还班时的调班记录");
                    return;
                }
                if (dtBackappDateChangeDrive.Rows.Count == 0)
                {
                    MessageBox.Show("查找不到代班司机还班时的调班记录");
                    return;
                }

                string backDateSourceDrive = dtBackappDateSourceDriver.Rows[0]["dcdr_cls_id"].ToString();

                string backDateChangeDrive = dtBackappDateChangeDrive.Rows[0]["dcdr_cls_id"].ToString();

                if (backDateChangeDrive == "0")
                {
                    if (backDateSourceDrive == "0")
                    { }
                    else
                    {
                        MessageBox.Show("还班时调班司机为早班，不能申请换班");
                        return;
                    }
                }
                if (backDateChangeDrive == "1")
                {
                    MessageBox.Show("还班时调班司机为休班，不能申请调班");
                    return;
                }
                if (backDateChangeDrive == "2")
                {
                    if (backDateSourceDrive != "1")
                    {
                        if (backDateSourceDrive != "2")
                        {
                            MessageBox.Show("还班时白班只能与休班司机换班");
                            return;
                        }
                    }
                }
                if (backDateChangeDrive == "3")
                {
                    if (backDateSourceDrive != "0")
                    {
                        if (backDateSourceDrive != "3")
                        {
                            MessageBox.Show("还班时晚班只能与早班司机换班");
                            return;
                        }
                    }
                }

            }

            //判断这个月是否有换班记录
            DataTable dtApplySourceDrive = _viewMode.GetSwitchRecordByMonth(Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd"), cmbDriver.SelectedValue.ToString());

            DataTable dtApplyTagerDrive = _viewMode.GetSwitchRecordByMonth(Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd"), cmbReplaceDriver.SelectedValue.ToString());

            List<string> lst = new List<string>();
            if (dtApplySourceDrive.Rows.Count > 0)
            {
                lst.Add("申请调班司机已存在调班记录");
            }
            if (dtApplyTagerDrive.Rows.Count > 0)
            {
                lst.Add("替班司机已存在调班记录");
            }

            //if (isBlackClass.IsChecked == true)
            //{
            //    DataTable dtBackSourceDrive = _viewMode.GetSwitchRecordByMonth(Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd"), cmbDriver.SelectedValue.ToString());

            //    DataTable dtBackTagerDrive = _viewMode.GetSwitchRecordByMonth(Convert.ToDateTime(dpTagerDay.SelectedDate).ToString("yyyy-MM-dd"), cmbReplaceDriver.SelectedValue.ToString());


            //    if (dtBackSourceDrive.Rows.Count > 0)
            //    {
            //        lst.Add("还班时申请调班司机已存在调班记录");
            //    }
            //    if (dtBackTagerDrive.Rows.Count > 0)
            //    {
            //        lst.Add("还班时替班司机已存在调班记录");
            //    }
            //}

            if (lst.Count > 0 && appDateSourceDrive != appDateChangeDrive)
            {
                lst.Insert(0, "是否继续调班？");
                MessageBoxResult dr;
                dr = MessageBoxHelper.ShowYesNoAndTips(lst);
                if (dr == MessageBoxResult.Yes)
                { }
                else
                {
                    return;
                }
            }
          
         
            //如果是今天的并且是同组司机 那么执行临时调班的函数
            if (sourceDate == DateTime.Now.ToString("yyyy-MM-dd") && appDateSourceDrive==appDateChangeDrive)
            {
                DriverCard sourceDrvierCard = new DriverCard();
                DriverCard targeDrvierCard = new DriverCard();


                _viewMode.setDriverCard(ref sourceDrvierCard,ref targeDrvierCard, cmbAppCls.SelectedValue.ToString(), cmbTAppcls.SelectedValue.ToString());
                try
                {
                    if (_viewMode.ExchangeDriver(sourceDrvierCard, targeDrvierCard))
                    {
                        MessageBox.Show("调班成功");
                    }
                    this.Close();

                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("调班失败"+ex.ToString());
                    this.Close();
                    return;
                }
                
            }

            try
            {
               // _viewMode.ExchangeDriverNew(cmbDriver.SelectedValue.ToString(), cmbReplaceDriver.SelectedValue.ToString(), sourceDate, tagerDate, txtReson.Text);
                string strAppBackcls = "";
                string strTAppBackcls = "";
                if (cmbAppBackcls.SelectedValue!= null)
                {
                    strAppBackcls = cmbAppBackcls.SelectedValue.ToString();
                }
                if (cmbTAppBackcls.SelectedValue!= null)
                {
                    strTAppBackcls = cmbTAppBackcls.SelectedValue.ToString();
                }

                if (_viewMode.ExchangeDriverNew(cmbAppCls.SelectedValue.ToString(), cmbTAppcls.SelectedValue.ToString(), strAppBackcls, strTAppBackcls, sourceDate, tagerDate, txtReson.Text,cmbDriver.SelectedValue.ToString(),cmbReplaceDriver.SelectedValue.ToString()))
                {

                    MessageBox.Show("调班成功");
                    if (sourceDate == DateTime.Now.ToString("yyyy-MM-dd") || tagerDate == DateTime.Now.ToString("yyyy-MM-dd"))
                    {
                        _viewMode.InitCardsCollections();
                       
                    }
                }
            }
            catch (Exception ex)
            {
                Helpers.SYCWPBGlobalvariables.globalvariables.log.writeError(ex.ToString());
                MessageBox.Show("调班失败");
            }

          //  this.Close();
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {

        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            if (cmbDriver.SelectedValue == null)
            {
                MessageBox.Show("请选择取消申请调班人");
                return;
            }
            if (dpSourceDay.SelectedDate == null)
            {
                MessageBox.Show("请选择取消调班日期");
                return;
            }
            ArrayList lstSql = new ArrayList();
            string res=_viewMode.CancelBackExchangeDriver(cmbDriver.SelectedValue.ToString(), Convert.ToDateTime(dpSourceDay.SelectedDate).ToString("yyyy-MM-dd"),ref lstSql);
            if (res=="3")
            {

                _viewMode.ExecuteSqlTran(lstSql);
                MessageBox.Show("取消调班成功");

            }
            else if(res=="1")
            {
                MessageBox.Show("当天该司机没有调班记录！");
            }
            else if (res == "2")
            {
                MessageBox.Show("当天没有还班记录！");
            }
        }

       
    }
}
